package it.giuseppetrisciuoglio.lyrics.http;

import it.giuseppetrisciuoglio.lyrics.persistence.entity.CacheResponse;
import it.giuseppetrisciuoglio.lyrics.persistence.repository.CacheResponseRepository;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class HttpResponseCacheHandler {
	
	@Autowired
	private CacheResponseRepository cacheResponseRepository;
	
	public void saveCacheResponse(String url, String html, int statusResponse){
		CacheResponse cacheResponse = cacheResponseRepository.findOne(url);
		if (cacheResponse != null){
			cacheResponse = new CacheResponse();
		}
		cacheResponse.setPayload(html);
		cacheResponse.setStatusResponse(statusResponse);
		cacheResponse.setUrl(url);
		cacheResponseRepository.saveAndFlush(cacheResponse);
	}
	
}
